<template>
  <div class="msg_box">
    <div :class="{ active: getFocus, errStyle: errStyle }" class="input" @click="getFocus = true">
      <label :class="{ L_active: getFocus || inputvalue }" :for="msg.admin">{{
        status === '' ? msg.title : msg.title[status]
      }}</label>
      <input
        ref="inputF"
        @focus="getfocus()"
        @blur="changeValue()"
        :type="msg.type"
        :name="msg.name"
        v-model="inputvalue"
      />
    </div>
    <slot />
  </div>
</template>

<script>
import { ref, watch } from 'vue'

import { UseLoginStore } from '../../stores/request'
import { UseAuthCode } from '@/stores/utils'
import { bus } from '../../utils/utils'

export default {
  name: 'InputMsg',
  props: {
    msg: {
      default: {}
    },
    status: {
      default: ''
    }
  },
  setup(props) {
    // 获取焦点
    const getFocus = ref(false)
    // 元素节点
    const inputF = ref()
    const inputvalue = ref(props.msg.test)
    const LoginStore = UseLoginStore()
    const AuthCode = UseAuthCode()
    const errStyle = ref(false)
    const max_length = ref(10)

    bus.on('errStyle', (errmsg) => {
      if (errmsg !== props.msg.name) return
      errStyle.value = true
    })

    function getfocus() {
      getFocus.value = true
      errStyle.value = false
    }

    function changeValue() {
      if (inputvalue.value === '') {
        errStyle.value = true
      }
      getFocus.value = false
      switch (props.msg.name) {
        case 'password':
          LoginStore.LoginOption.password = inputvalue.value
          LoginStore.SignInOption.password = inputvalue.value
          max_length.value = 16
          break
        case 'authcode':
          AuthCode.yanzhen = inputvalue.value.toLowerCase()
          max_length.value = 6
          break
        case 'admin':
          LoginStore.LoginOption.userNameOrEmail = inputvalue.value
          LoginStore.SendOption.name = inputvalue.value
          LoginStore.SignInOption.userName = inputvalue.value
          break
        case 'email':
          LoginStore.SendOption.sendTo = inputvalue.value
          LoginStore.SignInOption.email = inputvalue.value
          max_length.value = 50
          break
      }
    }
    changeValue()

    watch(getFocus, () => {
      if (!getFocus.value) return
      inputF.value.focus()
    })

    return {
      inputF,
      getFocus,
      inputvalue,
      changeValue,
      errStyle,
      getfocus,
      max_length
    }
  }
}
</script>
<style lang="scss" scoped>
$radius: 6px;
.msg_box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: end;
}
.input {
  flex: 1;
  position: relative;
  padding-top: 16px;
  box-sizing: border-box;
  width: 100%;
  border-bottom: 1px solid $font_unactive;
  display: flex;
  justify-content: center;
  &::after {
    position: absolute;
    bottom: 0;
    transition: all 0.25s;
    display: block;
    content: '';
    height: 1px;
    width: 0;
    background-color: $primary;
  }
  label {
    cursor: pointer;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    color: $font_unactive;
    transition: all 0.25s;
  }
  .L_active {
    transition: all 0.25s;
    font-size: 12px;
    line-height: 12px;
    top: 16px;
  }
  input {
    width: 100%;
    padding: 14px 0 8px 0;
    box-sizing: border-box;
    background-color: transparent;
    border: none;
    outline: none;
    color: $font;
  }
}
.active {
  // border-bottom: 1px solid $primary;
  &::after {
    transition: all 0.25s;
    width: 100%;
  }
}
.errStyle {
  transition: all 0.25s;
  border-bottom: 1px solid $warning;
  label {
    color: $warning;
    animation: err-label 0.36s ease;
  }
  input {
    color: $warning;
  }
  @keyframes err-label {
    40% {
      margin-left: 2px;
    }
    60%,
    80% {
      margin-left: -2px;
    }
    70%,
    90% {
      margin-left: 2px;
    }
  }
}
</style>
